package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.BaseContest;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private IShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R addShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        //1.判断当前点的菜,购物车中是否已经存在?
        // select * from setmeal where dish_id =? and user_id =?
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        if (shoppingCart.getSetmealId() != null) {
            qw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        } else {
            qw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }
        qw.eq(ShoppingCart::getUserId, BaseContest.getCurrentId());
        ShoppingCart oneFromSql = shoppingCartService.getOne(qw);
        if (oneFromSql == null) {
            //2.如果存在,说明点过了.数量加1
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(BaseContest.getCurrentId());
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        } else {
            //3.如果不存在,没有点过.新增
            oneFromSql.setNumber(oneFromSql.getNumber() + 1);
            shoppingCartService.updateById(oneFromSql);
            return R.success(oneFromSql);

        }


    }

    @GetMapping("/list")
    public R list(HttpSession session) {
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, BaseContest.getCurrentId());
        List<ShoppingCart> list = shoppingCartService.list(qw);
        return R.success(list);

    }

    @PostMapping("/sub")
    public R subShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        // number -1
        //update xxx set number = ?
        //where (dish_id =?或者 setmeial_id = ?)and user_id =?
        Long userId = BaseContest.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        if (shoppingCart.getDishId() != null) {
            qw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
            }else {
            qw.eq(ShoppingCart::getUserId, BaseContest.getCurrentId());
            }
            qw.eq(ShoppingCart::getUserId,userId);
            ShoppingCart one = shoppingCartService.getOne(qw);
            one.setNumber(one.getNumber() - 1);
            if (one.getNumber() == 0) {
                shoppingCartService.removeById(one.getId());
            } else {
                shoppingCartService.updateById(one);
            }
        return R.success(one);
        }
    @DeleteMapping("/clean")
    public R<String> clean(){

        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContest.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }
}






